<?php
namespace App\HttpController;

use EasySwoole\Queue\Job;
use EasySwoole\Http\AbstractInterface\Controller;
use App\Models\AdminModel;
use App\Models\UserModel;
use EasySwoole\Component\Di;
use App\HttpController\UserBase;
use App\Models\Project;
use App\service\MyQueue;
use App\Models\Assign;
use App\Models\History;
use App\Models\Fileart;
use App\Models\FileApp;
use App\Models\Article;
class Ds  extends UserBase
{
    
        
    public function onRequest(?string $action): ?bool
    {
        if (!parent::onRequest($action)) {
            return false;
        }


       if(!in_array($this->who['type'],[6])){
        
               $this->writeJson(403,"u r not permitted!");
               return false;

       }
        return true;
    }



    function publish(){
      $data = $this->request()->getRequestParam();
      $data['addedby'] = $this->who['id'];
      $data['addtime'] = time();

      try {
        // 开启事务
        \EasySwoole\ORM\DbManager::getInstance()->startTransaction();
        $model = Article::create($data);
        $res = $model->save();
        if ($res>0)  {
            $files = $data['file'];
            if($files){
            $datafiles = [];
            foreach($files as $k=>$v){
                array_push($datafiles,[
                    "path"=>$v['filepath'],
                    "articleid"=>$res,
                    "addtime"=>time()

                ]);
            }
            Fileart::create()->saveall($datafiles,true,false);
          }
        }

        \EasySwoole\ORM\DbManager::getInstance()->commit();

        $this->writeJson(200,['code'=>200],"ok");
    } catch(\Throwable  $e){
        // 回滚事务
        \EasySwoole\ORM\DbManager::getInstance()->rollback();
        var_dump($e->getMessage());
        $this->writeJson(200,['code'=>500,'msg'=>'数据库异常']);
    }
    }





  function list(){
    $limit = \EasySwoole\EasySwoole\Config::getInstance()->getConf('pagelimit');
    $model = Project::create();
    $page = $this->input('page',1);
    $owner = $this->input("owner","");
    $lxnumber = $this->input("lxnumber","");
    $name = $this->input("name","");
    $wtdw = $this->input("wtdw","");
    $bzjg = $this->input("bzjg","");
    $addtime = $this->input("addtime","");
    $industry = $this->input("industry","");
    $wtdwcontactor = $this->input("wtdwlxr","");
    $pslb = $this->input("pslb","");
    $where = [];
    if($owner == 'null'){
        $owner = "";
    }

    if($industry == 'null' || $industry == "1"){
        $industry = "";
    }

    if($lxnumber == 'null'){
        $lxnumber = "";
    }
    if($name == 'null'){
        $name = "";
    }
    if($wtdw == 'null'){
        $wtdw = "";
    }
    if($bzjg == 'null'){
        $bzjg = "";
    }
    if($pslb == 'null'){
        $pslb = "";
    }
    if($wtdwcontactor == 'null'){
        $wtdwcontactor = "";
    }
   

    if($owner){
        $model = $model->where("owner","%".$owner."%",'like');
    }

    if($lxnumber){
   
        $model = $model->where("lxnumber","%".$lxnumber."%",'like');
    }
    if($name){
        $model = $model->where("name","%".$name."%",'like');
    }
    if($wtdw){
        $model = $model->where("wtdw","%".$wtdw."%",'like');
    }
    if($bzjg){
        $model = $model->where("bzjg","%".$bzjg."%",'like');
    }
    
    if($industry){
        $model = $model->where(["industry"=>$industry]);
    }

    if($pslb && $pslb!='1'){
        $model = $model->where(['approve_type'=>$pslb]);
    }
    if($wtdwcontactor){
        $model = $model->where("wtdwcontactor","%".$wtdwcontactor."%",'like');
    }

    
          $st = $this->input("st");
          if($st=="done"){
            $list = $model->where('status',5,'>');
          }else{
            $list = $model->where(['status'=>5]);
          }
    

          $model = $model->order('id', 'DESC')->limit($limit * ($page - 1), $limit)->withTotalCount();
          $list = $model->all(null);
          $result = $model->lastQueryResult();
          $total = $result->getTotalCount();
          $pagecon = $this->showPageStringAsAJAXForitem("jumpage",$page,$limit,$total,$owner);
          $this->writeJson(200,['list'=>$list,'pg'=>$pagecon]);
  }





  function detail(){
    $data = $this->request()->getRequestParam();
    $id = intval($data['id']);
    $log = History::create()->join('user_list','user_list.id = approve_history.userid')->field(["approve_history.*","user_list.displayname"])->all(['itemid'=>$id]);
    $item  =Project::create()->get(['id'=>$id]);
    for($i=0;$i<count($log);$i++){
      $log[$i]['info'] = UserModel::create()->get(['id'=>$log[$i]['userid']]);
  
    $log[$i]['file'] = \App\Models\FileApp::create()->all(['approveid'=>$log[$i]['id']]);
    
  
    }
    $this->writeJson(200,['item'=>$item,'logs'=>$log]);
  }

  function dealitem(){
    $data = $this->request()->getRequestParam();
    $id = intval($data['id']);
    $item  =Project::create()->get(['id'=>$id]);
    if ($item['status'] != 5) {
        $this->writeJson(200,['code'=>500],'this project has a error status');
        return false;
    }
    try {
        // 开启事务
      $isopen  =   \EasySwoole\ORM\DbManager::getInstance()->startTransaction();
        if($data['appres'] == 1){
            $status = 6;
        }else{
            $status = -1;
        }
        // 执行更新 $model 的更新操作
        $res=$this->logapprove($this->who['id'],$data['appres'],$id,$data['idea'],0,$this->who['type']);
 
        
      $file2 = $this->input("files");
        
      if($file2){
          $datafiles = [];
      foreach($file2 as $k=>$v){
          array_push($datafiles,[
              "name"=>$v['filename'],
              "path"=>$v['filepath'],
              "approveid"=>$res,
              "userid"=>$this->who['id'],
              "time"=>time(),
          ]);
      }
 
      FileApp::create()->saveall($datafiles,true,false);

    }
        Project::create()->update(['status'=>$status],['id'=>$id]);
        // 提交事务
        \EasySwoole\ORM\DbManager::getInstance()->commit();

        $this->writeJson(200,['code'=>200],"ok");
    } catch(\Throwable  $e){
        // 回滚事务
        \EasySwoole\ORM\DbManager::getInstance()->rollback();
        var_dump($e->getMessage());
        $this->writeJson(404,"error");
    }

  }



   
} 
